<?php

class Application_Model_DbTable_Carreras extends Zend_Db_Table_Abstract
{

    protected $_name = 'carreras';
    
    //obtener cursos
    //$this->findDependentRowset('Application_Model_DbTable_Cursos');
    protected $_dependentTables = array('Application_Model_DbTable_Cursos');
    
    public function obtenerSigId()
    {   
        // necesita el nombre de la base de datos
        Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
        $dbName = Zend_Registry::get('dbName');
        $sql = "SELECT auto_increment"
                . " FROM information_schema.tables"
                . " WHERE table_schema='" . $dbName . "'"
                . " AND table_name='carreras';";  
        $query = $this->getAdapter()->query($sql);
        $last_id = $query->fetchAll();
        $sig_id = $last_id[0]['auto_increment'];
        return $sig_id; 
    }  

    public function obtenerTodo() {
        $select = $this->select();
        $select->from($this, array('id','nombre','descripcion','imagen',
            'fecha_inicio','horario','duracion','costo'));
        return $this->fetchAll($select);
    }
    
    public function crearCarpeta ($id){
        $upload = Zend_Registry::get('upload_paths');
        $pathname = $upload->paths->upload->images->carreras . "/" . $id;
        mkdir($pathname);
        return $pathname;
    }
    
    public function guardarImagen ($id,$imagen){
        $upload = Zend_Registry::get('upload_paths');
        $pathname = $upload->paths->upload->images->carreras . "/" . $id;
        if ($imagen) {
            $handle = opendir($pathname);
            while ($f = readdir($handle))  {   
                if ($f=='.' || $f=='..' || $f==$imagen) 
                    continue; 
                else 
                    unlink($pathname . "/" . $f); 
            }
            closedir($handle);
            
            $data = array(
                'imagen' => '/media/carreras/' . $id . '/' . $imagen,
            );
            $this->update($data, array('id = ?' => $id));
        } 
    }
    
    public function crearCarrera($nom,$des,$fecha,$horario,$dur,$costo) {
        $data = array(
            "nombre" => $nom, 
            "descripcion" => $des, 
            "fecha_inicio" => $fecha, 
            "horario" => $horario, 
            "duracion" => $dur, 
            "costo" => $costo
        );
        $id = $this->insert($data);
        return $id;       
    }
    
    public function obtenerCarrera($id) {
        $select = $this->select();
        $select->where('id = ?', $id);
        return $this->fetchRow($select);  
    }
    
    public function actualizarCarrera($id,$opciones) {
        $pathname = $opciones['path'];
        $newFile = $opciones['imagen'];
        if ($newFile) {
            $handle = opendir($pathname);
            while ($f = readdir($handle))  {   
                if ($f=='.' || $f=='..' || $f==$newFile) 
                    continue; 
                else 
                    unlink($pathname . "/" . $f); 
            }
            closedir($handle);
            $path_bd = '/media/carreras/' . $id . '/' . $opciones['imagen'];
        } else {
            $carrera = $this->obtenerCarrera($id);
            $path_bd = $carrera->imagen;
        }
        $data = array(
            'nombre' => $opciones['nombre'],
            'descripcion' => $opciones['descripcion'],
            'imagen' => $path_bd, 
            "fecha_inicio" => $opciones['fecha'], 
            "horario" => $opciones['horario'], 
            "duracion" => $opciones['duracion'], 
            "costo" => $opciones['costo']
        );
        $this->update($data, array('id = ?' => $id));
    }
    
    public function borrarCarrera($id){
        $upload = Zend_Registry::get('upload_paths');
        $pathname = $upload->paths->upload->images->carreras . "/" . $id;
        $handle = opendir($pathname);
        while ($f = readdir($handle))  {   
            if ($f=='.' || $f=='..') 
                continue; 
            else 
                unlink($pathname . "/" . $f); 
        }
        closedir($handle);
        rmdir($pathname);
        $this->delete(array('id = ?' => $id));
    }

}

